#!/bin/env/python
# -*- coding: utf8 -*-

'''Unit tests of jsonizer.py'''

import datetime
import unittest
import jsonizer


class TestJsonizer(unittest.TestCase):

  def testIdentity(self):
    data = ('s', {'a': 7}, (3,5,7), [8,2,0])
    for item in data:
      self.assertEquals(item, jsonizer.identity(item))

  def testConvBool(self):
    data = ((True, 'true'), (False, 'false'))
    for k, v in data:
      self.assertEquals(v, jsonizer.conv_bool(k))
    
  def testConvDatetime(self):
    data = ((datetime.datetime(2009, 10, 10), '2009-10-10T00:00:00'),
            (datetime.datetime(2009, 10, 10, 23), '2009-10-10T23:00:00'), 
            (datetime.datetime(2009, 10, 10, 23, 59), '2009-10-10T23:59:00'), 
            (datetime.datetime(2009, 10, 10, 23, 59, 20), '2009-10-10T23:59:20')
           )
    for k, v in data:
      self.assertEquals(v, jsonizer.conv_datetime(k))

  def testConvAutodetect(self):
    data = ((datetime.datetime(2009, 10, 10), '2009-10-10T00:00:00'),
            (True, 'true'), (False, 'false'), (3, 3), ('sth', 'sth'),
            ({'k1': 5, 3: True}, {'k1': 5, 3: 'true'})
           )
    for k, v in data:
      self.assertEquals(v, jsonizer.conv_autodetect(k))

  def testConvDict(self):
    self.assertEquals({'k1': 'v1', 'k2': 'false', 'k3': 'true',
                       'k4': '2009-11-20T01:01:00'},
                      jsonizer.conv_dict_values(
                       {'k1': 'v1', 'k2': False, 'k3': True,
                        'k4': datetime.datetime(2009,11,20,1,1,0)}))

if __name__ == '__main__':
  unittest.main()

